#!/bin/bash
# 项目打包
# 在build文件夹下生成名为facelog-linux-${facelog.version}-${webredis.version}-${fdevmgr.version}-${fdevmgrweb}.tar.bz2 的压缩包

sh_folder=$(dirname $(readlink -f $0))
cd $sh_folder

[ ! -f ./release.status ] && echo "not build yet, run release.sh firstly(项目还没有正常编译,请先执行release.sh编译所有项目)" && exit 255

# 导入release状态变量
. ./release.status
# 根据release.status中记录的状态判断对应的项目是否已经正常编译,没有则报错退出脚本
[ -z "$FACELOG_VERSION"    ] && echo "facelog not build yet, run './release.sh -b facelog' to build"                            && exit 255
[ -z "$WEBREDIS_VERSION"   ] && echo "webredis not build yet, run './release.sh -b webredis' to build"                          && exit 255
[ -z "$FDEVMGR_VERSION"    ] && echo "fdevmgrWhe not build yet, run './release.sh -b fdevmgrWhe' to build"                      && exit 255
[ -z "$FDEVMGRWEB_VERSION" ] && echo "fafdevmgrWhe-web celog not build yet, run './release.sh -b fafdevmgrWhe-web' to build"    && exit 255

# 如果 build 文件不存在则创建
[ -d build ] || mkdir build || exit 255

# 生成tar包的位置(相对路径)
tar_file=build/facelog-linux-$FACELOG_VERSION-$WEBREDIS_VERSION-$FDEVMGR_VERSION-$FDEVMGRWEB_VERSION.tar

echo "crate distributable package (创建发行包)..."

tar cvf $tar_file facelog-service || exit 255
tar rvf $tar_file webredis  --exclude=node_modules || exit 255
# 排除可能产生的临时文件夹~
tar rvf $tar_file fdevmgrWhse  --exclude=~ || exit 255
tar  rf $tar_file fdevmgrWhse-web || exit 255

tar rvf $tar_file *.init || exit 255
tar rvf $tar_file *.service || exit 255
tar rvf $tar_file constant || exit 255
tar rvf $tar_file facelog --exclude=log || exit 255
tar rvf $tar_file fdevmgr || exit 255
tar rvf $tar_file LICENSE || exit 255
tar rvf $tar_file *.sh --exclude=release.sh --exclude=dist.sh || exit 255
tar rvf $tar_file README.pdf || exit 255

echo "生成压缩包 $tar_file.bz2 ..."

bzip2 -f $tar_file || exit 255

echo "$tar_file.bz2 be created(发行包已生成)"
